<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <title>#(tablemeta.remarks)管理</title>
  <link rel="stylesheet" href="${ctx}/static/admin/layui/css/layui.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/sidebar.css">
  <link rel="stylesheet" href="${ctx}/static/admin/css/switch.css">
</head>
  <body>
    <div class="layui-card-header">
      <form class="layui-form">
	       <div class="layui-form-item">
	           <div class="layui-input-inline" style="top: 3px;">
			       <input type="search" name="name" autocomplete="off" class="layui-input" style="width:180px;" placeholder="名称"/>
		       </div>
	           <div class="layui-input-inline">
			       <button class="layui-btn" lay-submit lay-filter="serach_form"><i class="layui-icon">&#xe615;</i> 查询</button>
		       </div>
	       </div>
        </form>
    </div>
	<div class="layui-card-body">
		<button id="add" class="layui-btn layui-btn-sm">新增</button>
		<button id="export" class="layui-btn layui-btn-sm">导出</button>
		<table id="#(classNameSmall)table" lay-filter="#(classNameSmall)"></table>
	</div>
  </body>
</html>
<script src="${ctx}/static/admin/layui/layui.js"></script>
<script src="${ctx}/static/admin/js/common.js"></script>
<script type="text/html" id="toolbar">
  <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
  <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
<script type="text/html" id="enabled">
  <input type="checkbox" name="status" lay-filter="status" value="{{d.status}}" lay-skin="switch" lay-text="启用|禁用" {{ d.status == 1 ? 'checked' : '' }}>
</script>
<script type="text/javascript">
var tableIns;
var params = {};
layui.use(['table','layer','jquery','element','sidebar','form'], function(){
	var $ = jQuery = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var element = layui.element;
	var sidebar = layui.sidebar;
    var form = layui.form;
	tableIns = table.render({
		elem: '##(classNameSmall)table',
		cellMinWidth: 50,
		size:'sm',
		even:true,
	    url: '${ctx}/#(classNameSmall)/getList',
		height:'full-113',
	    page: true,
	    cols: [[
		  {type: 'numbers'},
		  #for(x : tablemeta.columnMetas)
		  {field: '#(x.name)', title: '#(x.remarks)', width:120},
	      #end
		  {field: 'status', title:'状态', templet: '#enabled', unresize: true, width:100},
	      {title: '操作', width: 178, fixed: 'right', align:'center',toolbar: '#toolbar'}
	    ]],
	    done:function(res, curr){
	      params['curr'] = curr;
	    }
	});
	table.on('tool(#(classNameSmall))', function(obj){
	    var data = obj.data;
	    if(obj.event === 'del'){
	    	layer.confirm('确定删除行么？', function(index){
	    	  $.post('${ctx}/#(classNameSmall)/delete', {"ids":data.id}, function(result) {
	  			if (result.success) {
	  				obj.del();
	  				layer.close(index);
	  			} else {
	  				layer.msg(result.msg);
	  			}
	  		  }, 'json');
	      });
	    } else if(obj.event === 'edit'){
	    	layer.open({
				type: 2,
				area: ['700px', '450px'],
				fixed: false, //不固定
				maxmin: true,
				content: '${ctx}/#(classNameSmall)/form?id='+data.id,
		  		btn: ['保存', '关闭'],
				yes: function(index, layero){
	                var iframeWin = layero.find('iframe')[0];
	                iframeWin.contentWindow.table = tableIns;
	                iframeWin.contentWindow.params = params;
	                layero.find('iframe').contents().find('#submit-btn').click();
			  	},
			  	btn3: function(index, layero){
		  			var index = layer.getFrameIndex(window.name);
					layer.close(index);
			  	}
	  		});
	    }
	});
	
	form.on('switch(status)', function(data){
		var selectIfKey = data.othis;                                                
		var parentTr = selectIfKey.parents("tr");                                 
		var parentTrIndex = parentTr.attr("data-index");                          
		var id = parentTr.find(('td:eq(1)')).text().trim(); 
		var state = 0;
		if(data.elem.checked){state = 1;}
		$.post('${ctx}/#(classNameSmall)/setStatus', {ids: id, status: state}, function() {}, 'json');
	}); 
	
	$("#add").on('click',function(){
		layer.open({
			type: 2,
			area: ['700px', '450px'],
			fixed: false, //不固定
			maxmin: true,
			content: '${ctx}/#(classNameSmall)/form',
			btn: ['提交', '重置','取消'],
			yes: function(index, layero){
                var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = tableIns;
                iframeWin.contentWindow.params = params;
                layero.find('iframe').contents().find('#submit-btn').click();
		  	},
		  	btn2: function(index, layero){
		  		var iframeWin = layero.find('iframe')[0];
                iframeWin.contentWindow.table = tableIns;
                iframeWin.contentWindow.params = params;
                layero.find('iframe').contents().find('#reset-btn').click();
                return false;
		  	},
		  	btn3: function(index, layero){
	  			var index = layer.getFrameIndex(window.name);
				layer.close(index);
		  	}
		});
	})
	form.on('submit(serach_form)', function (data) {
         table.reload('#(classNameSmall)table', {
             method : 'post',
             where: {
                 name : data.field.name
             },
             page: {
			     curr : 1 //重新从第 1 页开始
			 }
         });
         params['name'] = data.field.name;
         return false;
     });
	$("#export").on('click',function(){
		window.open('${ctx}/#(classNameSmall)/exportExcel');
	})
	
});
</script>
